package br.com.ps.bombertournament.game.animation;

import java.util.ArrayList;
import java.util.List;

import br.com.ps.bombertournament.game.BaseAction;

import com.badlogic.gdx.graphics.g2d.TextureRegion;

/**
 * Represent the frame of a animation.
 * 
 * @author Rennan Stefan Boni - rennanboni@gmail.com
 */
public class Frame implements Cloneable {
	
	/* Texture to show */
	private TextureRegion texture;
	/* Max time the frame must be displayed */
	private float duration;
	/* List of action must be executed when the frame is displayed */
	private List<Action> actions;

	public TextureRegion getTexture() {
		return texture;
	}

	public void setTexture(TextureRegion texture) {
		this.texture = texture;
	}

	public float getDuration() {
		return duration;
	}

	public void setDuration(float duration) {
		this.duration = duration;
	}

	public List<Action> getActions() {
		return actions;
	}

	public void setActions(List<Action> actions) {
		this.actions = actions;
	}

	/**
	 * Reset all actions preparing to executed when the frame is displayed
	 */
	public void reset() {
		if (actions != null) {
			for (Action action : actions) {
				action.reset();
			}
		}
	}

	/**
	 * Dispose the frame.
	 */
	public void dispose() {
		if (texture != null && texture.getTexture() != null) {
			texture.getTexture().dispose();
		}
	}
	
	@Override
	public Frame clone() {
		try {
			Frame result = (Frame) super.clone();
			
			if (result.actions != null) {
				List<Action> cloneActions = new ArrayList<Action>(result.actions.size());
				for (int i = 0; i < result.actions.size(); i++) {
					cloneActions.add(i, ((BaseAction) result.actions.get(i)).clone());
				}
				result.actions = cloneActions;
			}
			
			return result;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
}
